package com.hewl.crud.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hewl.crud.bean.Employee;
import com.hewl.crud.bean.EmployeeExample;
import com.hewl.crud.bean.EmployeeExample.Criteria;
import com.hewl.crud.dao.EmployeeMapper;

@Service
public class EmployeeService {
	
	@Autowired
	EmployeeMapper employeeMapper;
	/**
	 *  查询所有员工
	 * @return
	 */
	public List<Employee> getAll() {
		// TODO Auto-generated method stub
		EmployeeExample example = new EmployeeExample();
		example.setOrderByClause("emp_id");
		return employeeMapper.selectByExampleWithDept(example);
	}
	/**
	 *  员工保存
	 * @param employee
	 */
	public void savaEmp(Employee employee) {
		employeeMapper.insertSelective(employee);
	}
	
	//检验用户名是否可用
	public boolean checkUser(String empName) {
		// TODO Auto-generated method stub
		EmployeeExample example = new EmployeeExample();
		Criteria criteria = example.createCriteria();
		criteria.andEmpNameEqualTo(empName);
		long count = employeeMapper.countByExample(example);
		return count == 0;
	}
	//按照id查询员工
	public Employee getEmp(Integer id) {
		// TODO Auto-generated method stub
		Employee employee = employeeMapper.selectByPrimaryKey(id);
		return employee;
	}
	//员工更新方法
	public void updateEmp(Employee empolyee) {
		// TODO Auto-generated method stub
		employeeMapper.updateByPrimaryKeySelective(empolyee);
	}
	//单个删除的方法
	public void deleteEmp(Integer id) {
		// TODO Auto-generated method stub
		employeeMapper.deleteByPrimaryKey(id);
	}
	//批量删除的方法
	public void deleteBatch(List<Integer> ids) {
		// TODO Auto-generated method stub
		EmployeeExample example = new EmployeeExample();
		Criteria criteria = example.createCriteria();
		//delete from xxx where emp_id in(1,2,3)
		criteria.andEmpIdIn(ids);
		employeeMapper.deleteByExample(example);
	}
	
}
